package com.julyup.core.interceptor;

import com.julyup.core.annotation.EnablePage;
import com.julyup.core.page.OrderType;
import com.julyup.core.page.PageAction;
import com.julyup.core.page.PageHolder;
import com.julyup.core.page.PageSearch;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * Page信息拦截封装
 *
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2018/10/22 3:11 PM
 */
@Slf4j
public class PageInterceptor implements HandlerInterceptor {


  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

    // 只拦截GET请求
    if (!"GET".equals(request.getMethod())) {
      return true;
    }
    if (!(handler instanceof HandlerMethod)) {
      return true;
    }

    HandlerMethod method = (HandlerMethod) handler;

    if (method.getMethodAnnotation(EnablePage.class) == null) {
      return true;
    }

    // 创建PageSearch对象存储Page信息
    PageSearch ps = new PageSearch();
    ps.setOpsType(request.getParameter("opsType") == null ?
        PageAction.LIST : PageAction.valueOf(request.getParameter("opsType")));

    // 查询参数
    HashMap<String, Object> pageParameter = new HashMap<>();
    // 范围参数
    HashMap<String, Object> rangeMaxParameter = new HashMap<>();
    HashMap<String, Object> rangeMinParameter = new HashMap<>();
    // 导出参数
    HashMap<String, Object> exportParameter = new HashMap<>();
    Enumeration<String> parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
      String parameterName = parameterNames.nextElement();

      if ("pageSize".equals(parameterName)) {
        ps.setPageSize(Integer.valueOf(request.getParameter(parameterName)));
        continue;
      }
      if ("pageNumber".equals(parameterName)) {
        int pi = Integer.valueOf(request.getParameter(parameterName)) - 1;
        ps.setPageNumber(pi < 0 ? 0 : pi);
        continue;
      }
      // 封装排序字段，排序参数为：key=DESC | key=ASC
      if (StringUtils.isNotEmpty(parameterName) && parameterName.startsWith("sort_")) {
        ps.setOrderType(OrderType.valueOf(request.getParameter(parameterName).toUpperCase()));
        ps.setOrderColumn(parameterName.substring(5));
        continue;
      }

      // 封装查询参数，以page_开头
      if (StringUtils.isNotEmpty(parameterName) && parameterName.startsWith("page_")) {
        if (StringUtils.isNotEmpty(request.getParameter(parameterName))) {
          pageParameter.put(parameterName.substring(5), request.getParameter(parameterName).trim());
        }
      }

      // 封装导出参数
      if (StringUtils.isNotEmpty(parameterName) && parameterName.startsWith("expo_")) {
        exportParameter.put(parameterName.substring(5), request.getParameter(parameterName));
      }

      // 封装范围
      if (StringUtils.isNotEmpty(parameterName) && parameterName.startsWith("max_")) {
        rangeMaxParameter.put(parameterName.substring(4), request.getParameter(parameterName));
      }
      if (StringUtils.isNotEmpty(parameterName) && parameterName.startsWith("min_")) {
        rangeMinParameter.put(parameterName.substring(4), request.getParameter(parameterName));
      }
    }
    ps.setPageParameter(pageParameter);
    ps.setExportParameter(exportParameter);
    ps.setRangeMaxParameter(rangeMaxParameter);
    ps.setRangeMinParameter(rangeMinParameter);
    PageHolder.setHolder(ps);
    return true;
  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  }
}
